package com.course.dao;

import java.util.List;

import com.course.domain.Department;
import com.course.domain.Specialty;

public class SpecialtyDao extends SupperDao
{

	@SuppressWarnings( "unchecked" )
	public long countOfSpecialty( int departmentId )
	{
		String hql = "select count(*) from Specialty where department.id = " + departmentId;
		List<Long> list = this.findByHQL( hql );
		return list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public List< Specialty > getSpecialties( int start, int length, int departmentId )
	{
		String hql = "from Specialty where department.id = " + departmentId;
		return this.findPaging( hql, start, length, null );
	}

	public Specialty getSpecialtyById( int specialtyId )
	{
		return ( Specialty )this.get( Specialty.class, specialtyId );
	}
	
	public boolean saveSpecialty( String name, Integer departmentId )
	{
		Specialty specialty = new Specialty();
		specialty.setName( name );
		specialty.setDepartment( (Department)this.get( Department.class, departmentId ) );
		return this.save( specialty );
	}

	public boolean updateSpecialty( Integer id, String name, Integer departmentId )
	{
		Specialty specialty = this.getSpecialtyById( id );
		specialty.setName( name );
		specialty.setDepartment( ( Department )this.get( Department.class, departmentId ) );
		return this.update( specialty );
	}

	public boolean deleteSpecialtyById( Integer id )
	{
		Specialty specialty = ( Specialty )this.get( Specialty.class, id );
		return this.delete( specialty );
	}
	
	@SuppressWarnings( "unchecked" )
	public Department getDepartmentBySpecialtyId( int specialtyId )
	{
		List list = null;
		String hql = "select d from Department d, Specialty s where s.department.id = d.id and s.id = " + specialtyId;
		list = ( List )this.findByHQL( hql );
		if(list.isEmpty())
			return null;
		return ( Department )list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public List< Specialty > getSpecialtiesByDepartmentId( int departmentId )
	{
		String hql = "from Specialty s where s.department.id = " + departmentId;
		return this.findByHQL( hql );
	}

	@SuppressWarnings( "unchecked" )
	public Specialty getSpecialtyByMyClassId( int myClassId )
	{
		List list = null;
		String hql = "select s from Specialty s, MyClass m where m.specialty.id = s.id and m.id = " + myClassId;
		list = ( List )this.findByHQL( hql );
		if(list.isEmpty())
			return null;
		return ( Specialty )list.get( 0 );
	}

	@SuppressWarnings( "unchecked" )
	public long countOfSpecialtyBySpecialtyName( String name )
	{
		String hql = "select count(*) from Specialty s where s.name = '" + name + "'";
		List<Long> list = this.findByHQL( hql );
		return list.get( 0 );
	}
	
	@SuppressWarnings( "unchecked" )
	public List< Specialty > getSpecialtiesBySpecialtyName( int start, int length, String name )
	{
		String hql = "from Specialty s where s.name = '" + name + "'";
		return this.findPaging( hql, start, length, null );
	}

}
